package controllers

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"fmt"
	"github.com/kataras/iris/v12"
	"time"
)

type EnrollPlan struct {
	Ctx iris.Context
}

// 获取大学招生计划列表
func (c *EnrollPlan) PostList() interface{} {
	id := c.Ctx.PostValue("id")

	if len(id) <= 0 {
		return utils.CommonRes(1000, "请提供大学id", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	year := time.Now().Format("2006")
	sqlStr := fmt.Sprintf("select any_value(id) as id from enrollment_plan where university_id=%s and year=%s group by batch", id, year)
	list, err := db.QueryString(sqlStr)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取招生计划列表：%s", err.Error()), "")
	}

	batchMap := map[int]string{
		0: "未知",
		1: "本科一批",
		2: "本科二批",
		3: "高职高专",
	}
	enrollPlanList := make([]*models.EnrollmentPlan, 0)
	for i, _ := range list {
		enrollPlan := new(models.EnrollmentPlan)
		_, err = db.ID(list[i]["id"]).Get(enrollPlan)
		if err != nil {
			return utils.CommonRes(1000, fmt.Sprintf("无法获取招生计划列表：%s", err.Error()), "")
		}
		enrollPlan.BatchShow = batchMap[enrollPlan.Batch]
		enrollPlanList = append(enrollPlanList, enrollPlan)
	}

	return utils.CommonRes(20000, "成功获取招生计划列表", enrollPlanList)
}
